//
//  MstrangdetailsViewController.m
//  CheekiOS
//
//  Created by mac on 2017/5/24.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "MstrangdetailsViewController.h"
#import "MJPhotoBrowser.h"
#import <AVFoundation/AVFoundation.h>


@interface MstrangdetailsViewController ()<CAAnimationDelegate>

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIButton *backBtn;//返回
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *textLab;//内容
@property (nonatomic, strong) UILabel *createTimeLab;//时间
@property (nonatomic, strong) NSArray *arrimg;//图片数组

@property (nonatomic, strong) AVPlayer *player;//播放器

@property (nonatomic, strong) AVPlayerLayer *playerLayer;//播放器

@property (nonatomic, strong) MJPhotoBrowser *browser;



@end

@implementation MstrangdetailsViewController




- (void)viewDidLoad {
    [super viewDidLoad];

}
- (void)backButtonClick{
    [_player pause];
    _player=nil;
    CABasicAnimation *animation = nil;
    //缩放
    animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    [animation setFromValue:@1.0];//设置起始值
    [animation setToValue:@0.1];//设置目标值
    [animation setDelegate:self];//代理回调
    [animation setDuration:0.5];//设置动画时间，单次动画时间
    [animation setRemovedOnCompletion:NO];
    
    //you view need to replace
    [self.navigationController.view.layer addAnimation:animation forKey:nil];

    [UIView animateWithDuration:0.5 animations:^{
    } completion:^(BOOL finished) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
             
            [self.navigationController popViewControllerAnimated:NO];

        });
    }];
    
 
}

- (void)imageBackClick{
    _textLab.hidden= YES;
    _createTimeLab.hidden = YES;
    [self.navigationController popViewControllerAnimated:NO];


}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self
     selector:@selector(imageBackClick)
     name:@"photoOutMstrangDetatils"
     object:nil];
    [self setNavgationBarHidden];

    
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)setModel:(Active *)model{
    
       _model = model;
    _bgView = [[UIView alloc] init];
    _bgView.frame = CGRectMake(0, 0, KScreenWidth, 64);
    
    UILabel *lab = [[UILabel alloc] init];
    lab.frame = CGRectMake(_bgView.centerX-15, 25, KScreenWidth/2, 30);
    lab.text = @"详情";
    lab.textColor = ColorI(0xffffff);
    lab.font = FontP(15);
    [self.view addSubview:lab];
    [self.view addSubview:_bgView];

    _imgView = [[UIImageView alloc] init];
    _imgView.image = [UIImage imageNamed:@"back_w"];
    _imgView.frame = CGRectMake(15, 29.5, 13, 19);
    [_bgView addSubview:_imgView];
    
    _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backBtn addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [_backBtn setTitle:@"返回" forState:UIControlStateNormal];
    _backBtn.titleLabel.font = FontP(16);
    _backBtn.frame = CGRectMake(_imgView.right+2.5, 23, 40*SCREEN_WIDTH_RATIO47, 30*SCREEN_WIDTH_RATIO47);
    
    
    [_bgView addSubview:_backBtn];
    

    
        if (_model.img.length >0) {
            _arrimg = [[NSArray alloc] init];
            _arrimg = [_model.img componentsSeparatedByString:@","];
        }

    if (_model.video.length > 0) {
        if (  _model.content.length > 0 ) {
            _textLab = [[UILabel alloc] init];
            _textLab.font = FontP(13);
            _textLab.numberOfLines = 0;
            _textLab.textAlignment = NSTextAlignmentLeft;
            _textLab.text = _model.content;
            _textLab.textColor = [UIColor whiteColor];
            _textLab.numberOfLines = 0;
            [_textLab sizeToFit];
            _textLab.hidden = NO;
        }
        _bgView.backgroundColor = [UIColor blackColor];
        [_backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = self.view.frame;
        [btn addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        
        CGFloat Height = KScreenWidth/4*3;
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:HttpImgUrl(_model.video)]];
        AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:item];
        AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer: player];
        playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        playerLayer.frame = CGRectMake(0, 0, KScreenWidth, Height);
        playerLayer.position = self.view.center;
        [self.view.layer addSublayer: playerLayer];
        [playerLayer setNeedsDisplay];
        
        [player play];
        
        self.player = player;
        
        _playerLayer = playerLayer;
        
        self.view.backgroundColor = [UIColor blackColor];
        __weak typeof(self) weakSelf = self;
        NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
        [noteCenter addObserverForName:AVPlayerItemDidPlayToEndTimeNotification
                                object:nil
                                 queue:nil
                            usingBlock:^(NSNotification *note) {
                                [weakSelf.player seekToTime:kCMTimeZero];
                                [weakSelf.player play];
                            }];
        [self.view addSubview:_textLab];
        
        [_textLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.view).offset(-30);
            make.left.equalTo(self.view).offset(30);
            make.bottom.equalTo(self.view).offset(-54);
        }];
        
        [self.view addSubview:btn];
        
        
 
    }else{
        
        
        if (_arrimg.count > 0) {
            
            if (  _model.content.length > 0 ) {
                _textLab = [[UILabel alloc] init];
                _textLab.font = FontP(13);
                _textLab.textAlignment = NSTextAlignmentLeft;
                _textLab.numberOfLines = 0;
                [_textLab sizeToFit];
                _textLab.textColor = [UIColor whiteColor];
                _textLab.text = _model.content;
                
                _textLab.hidden = NO;
            }
            NSInteger count = _arrimg.count;
            // 1.封装图片数据
            NSMutableArray *photos = [NSMutableArray arrayWithCapacity:count];
            for (int i = 0; i<count; i++) {
                
                NSString *images = [self.arrimg objectAtIndex:i];
                NSString *url =HttpImgUrl(images);
                MJPhoto *photo = [[MJPhoto alloc] init];
                photo.url = [NSURL URLWithString:url]; // 图片路径
                [photos addObject:photo];
            }
            // 2.显示相册
            self.browser = [[MJPhotoBrowser alloc] init];
            self.browser.photos = photos; // 设置所有的图片
            [self.browser show];
            
            [self.browser.view addSubview:_textLab];

            [_textLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.browser.view).offset(-30);
                make.left.equalTo(self.browser.view).offset(30);
                make.bottom.equalTo(self.browser.view).offset(-54);
            }];
            _createTimeLab = [[UILabel alloc] initWithFrame:CGRectMake(_bgView.x, 9, KScreenWidth, 30)];
            _createTimeLab.text = _model.createTime;
            _createTimeLab.textColor = [UIColor whiteColor];
            _createTimeLab.font = FontP(15);
            _createTimeLab.textAlignment = NSTextAlignmentCenter;
            [self.browser.view addSubview:_createTimeLab];
            
           
        }else{
            _bgView.backgroundColor = Colorbackground;
            
            
            
            UILabel *linLab = [[UILabel alloc] initWithFrame:CGRectMake(0, _bgView.bottom, KScreenWidth, 1)];
            linLab.backgroundColor = TheLinColor;
            [_bgView addSubview:linLab];
            self.view.backgroundColor = [UIColor whiteColor];
            _createTimeLab = [[UILabel alloc] initWithFrame:CGRectMake(_bgView.x, 19, KScreenWidth, 30)];
            _createTimeLab.text = _model.createTime;
            _createTimeLab.textColor = [UIColor blackColor];
            _createTimeLab.font = FontP(15);
            _createTimeLab.textAlignment = NSTextAlignmentCenter;
            [_bgView addSubview:_createTimeLab];
            
            _imgView.image = [UIImage imageNamed:@"back"];
            
            [_backBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
            _textLab = [[UILabel alloc] init];
            _textLab.font = FontP(15);
            _textLab.numberOfLines = 0;
            _textLab.textAlignment = NSTextAlignmentLeft;
            _textLab.textColor = [UIColor blackColor];

            _textLab.text = _model.content;
            _textLab.hidden = NO;
            [self.view addSubview:_textLab];
            [_textLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.view).offset(-12);
                make.left.equalTo(self.view).offset(12);
                make.top.equalTo(self.view).offset(74);
            }];
            

        }
        
}
    

    
}


@end
